package com.hanzidong.cms.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//获取session
		HttpSession session = request.getSession();
		
		//判断是否有用户值
		Object object = session.getAttribute("user");
		
		//如果有则放行
		if(object != null) {
			//如果有登录的用户数据，则返回true，直接放行
			
			return true;
		}else {
			//如果没有则拦截，跳转到登录页面
			//请求转发，转发到登录页面
//			request.getRequestDispatcher("/user/toLogin").forward(request, response);
			
			//响应重定向
			response.sendRedirect("/user/toLogin");
			
			return false;
		}
		
	}
	
	
}
